package kt.com.bilin.coin.ui.activity

import android.view.View
import android.view.ViewGroup
import com.alibaba.android.arouter.facade.annotation.Route
import com.bilin.coin.R
import com.bilin.coin.databinding.ItemBsFunctionBinding
import com.bilin.coin.databinding.ItemBsZixuBinding
import com.bilin.coin.tools.AlertUtils
import com.yanzhenjie.sofia.Utils
import kotlinx.android.synthetic.main.activity_business_school.*
import kt.com.bilin.coin.bean.BusinessSchoolFunBean
import kt.com.bilin.coin.bean.BusinessSchoolZXBean
import kt.com.bilin.coin.common.*
import kt.com.bilin.coin.ui.adapter.BaseRecyclerAdapter
import kt.com.bilin.coin.ui.adapter.BaseViewHolder
import kt.com.bilin.coin.utils.FullyGridLayoutManager
import kt.com.bilin.coin.utils.FullyLinearLayoutManager

@Route(path = PATH_BUSINESS_SCHOOL)
class BusinessSchoolActivity : BaseActivity(R.layout.activity_business_school) {
    private val mBusinessSchoolFunctionAdapter: BusinessSchoolFunctionAdapter = BusinessSchoolFunctionAdapter()
    private val mBusinessSchoolZiXuAdapter: BusinessSchoolZiXuAdapter = BusinessSchoolZiXuAdapter()
    override fun initView() {
        Utils.invasionStatusBar(this)
        Utils.setStatusBarDarkFont(this, false)
        mBusinessSchoolFunctionAdapter.datas = mutableListOf<BusinessSchoolFunBean>().apply {
            add(BusinessSchoolFunBean(R.mipmap.bg_bs_new_notice, R.mipmap.icon_bs_new_notice, R.string.text_business_school_xuzhi.getString()))
            add(BusinessSchoolFunBean(R.mipmap.bg_bs_baike, R.mipmap.icon_bs_baike, R.string.text_business_school_baike.getString()))
            add(BusinessSchoolFunBean(R.mipmap.bg_bs_ketangx, R.mipmap.icon_bs_ketang, R.string.text_business_school_ketang.getString()))
            add(BusinessSchoolFunBean(R.mipmap.bg_bs_vedio, R.mipmap.icon_bs_vedio, R.string.text_business_school_video.getString()))
            add(BusinessSchoolFunBean(R.mipmap.bg_bs_sucai, R.mipmap.icon_bs_sucai, R.string.text_business_school_sucai.getString()))
            add(BusinessSchoolFunBean(R.mipmap.bg_bs_wenti, R.mipmap.icon_bs_wenti, R.string.text_business_school_wenti.getString()))
        }
        mBusinessSchoolFunctionAdapter.setOnClickAdapterListenter(::onClick)
        rvFunction.layoutManager = FullyGridLayoutManager(this, 2)
        rvFunction.adapter = mBusinessSchoolFunctionAdapter

        mBusinessSchoolZiXuAdapter.datas = mutableListOf<BusinessSchoolZXBean>().apply {
            add(BusinessSchoolZXBean(R.mipmap.icon_bs_zixunjishu, "比邻社区-百度", "2020-09-24 18:29", "https://baijiahao.baidu.com/builder/preview/s?id=10182098309048615033"))
            add(BusinessSchoolZXBean(R.mipmap.icon_bs_zixunshic, "比邻社区-搜狐", "2020-09-24 19:28", "https://www.sohu.com/a/420528678_120741239"))
            add(BusinessSchoolZXBean(R.mipmap.icon_bs_zixunyuny, "比邻社区-头条", "2020-09-14 16:08", "https://www.toutiao.com/item/6872266015133139468/"))
        }
        rvZixu.layoutManager = FullyLinearLayoutManager(this)
        rvZixu.adapter = mBusinessSchoolZiXuAdapter
    }

    private fun onClick(v: View, position: Int) {
        val mBusinessSchoolFunBean = mBusinessSchoolFunctionAdapter.datas[position]
        when (mBusinessSchoolFunctionAdapter.datas[position].bgIcon) {
            R.mipmap.bg_bs_ketangx -> ARouterNavigation(PATH_BS_AUDIO)
            R.mipmap.bg_bs_vedio -> ARouterNavigation(PATH_BS_VIDEO)
            R.mipmap.bg_bs_new_notice -> openUrl(URL_NOTICE, mBusinessSchoolFunBean.name)
            R.mipmap.bg_bs_baike -> openUrl(URL_WIKI, mBusinessSchoolFunBean.name)
            R.mipmap.bg_bs_wenti -> openUrl(URL_QA, mBusinessSchoolFunBean.name)
            R.mipmap.bg_bs_sucai -> ARouterNavigation(PATH_MATERIAL_SHARE)
        }
    }

    inner class BusinessSchoolFunctionAdapter : BaseRecyclerAdapter<BaseViewHolder<ItemBsFunctionBinding>, BusinessSchoolFunBean>() {
        override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(ItemBsFunctionBinding.inflate(getLayoutInflate(parent.context), parent, false))
        override fun onBindViewHolder(holder: BaseViewHolder<ItemBsFunctionBinding>, bean: BusinessSchoolFunBean, position: Int) {
            holder.mViewBinding.run {
                bean.run {
                    ivBs.setImageResource(bgIcon)
                    ivIcon.setImageResource(icon)
                    tvName.text = name
                    setViewClick(position, root)
                }
            }
        }
    }

    inner class BusinessSchoolZiXuAdapter : BaseRecyclerAdapter<BaseViewHolder<ItemBsZixuBinding>, BusinessSchoolZXBean>() {
        override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(ItemBsZixuBinding.inflate(getLayoutInflate(parent.context), parent, false))
        override fun onBindViewHolder(holder: BaseViewHolder<ItemBsZixuBinding>, bean: BusinessSchoolZXBean, position: Int) {
            holder.mViewBinding.run {
                bean.run {
                    ivImage.setImageResource(bgIcon)
                    tvTitle.text = title
                    tvTime.text = time
                    setViewClick(position, root)
                }
            }
        }

        override fun subOnClickAdapter(v: View, position: Int) {
            getItem(position).run { openUrl(link, title) }
        }
    }
}